package com.app.service;

import com.alibaba.fastjson.JSON;
import com.app.dao.mysql.YjDataMapper;
import com.app.domain.UploadParameter;
import com.app.domain.YjData;
import com.app.domain.ezmeb.*;
import com.app.domain.mslc.MslcResultJson;
import com.app.domain.mslc.Token;
import com.app.utils.HttpClients;
import com.app.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
@Transactional
public class EzmebService {
    @Autowired
    UploadParameterService uploadParameterService;
    @Autowired
    YjDataService yjDataService;
    @Autowired
    YjDataMapper yjDataMapper;

    public EzmData getEzmData(List<YjData> yjDataList, UploadParameter uploadParameter) {
        EzmData ezmData = new EzmData();
        ezmData.setDataKey(UUID.randomUUID().toString().replace("-",""));
        ezmData.setFacilityCode("YJ2-HZM-001");
        ezmData.setHandType("add");
        List<Data> dataList = getData(yjDataList, uploadParameter);
        ezmData.setData(dataList);
        return ezmData;
    }

    private List<Data> getData(List<YjData> yjDataList, UploadParameter uploadParameter) {
        List<Data> dataList = new ArrayList<>();
        Data data = new Data();
        data.setEquipmentCode("YJ2-HZM-001");
        data.setCrossCode(yjDataList.get(0).getBridge_code());
        data.setBridgeCode(yjDataList.get(0).getBridge_code());
        data.setBridgeName(uploadParameter.getGjjg());
        data.setCrossNO(Integer.parseInt(uploadParameter.getKongdaoshu()));
        data.setBeamLength(Integer.parseInt(uploadParameter.getLblx()));
        data.setPouringDate(uploadParameter.getBeiyong());
        data.setTensionDate(uploadParameter.getZlsj());
        data.setGroutingDate(yjDataList.get(0).getMudjack_date() + " " + yjDataList.get(0).getBegin_time());
        data.setOperator(uploadParameter.getMemo());
        data.setWaringStatus(0);
        List<TendonGrouting> tendonGroutingList = new ArrayList<>();
        data.setTendonGrouting(tendonGroutingList);
        dataList.add(data);
        for (YjData yjData : yjDataList) {
            TendonGrouting grouting = new TendonGrouting();
            grouting.setSteelStrandName(yjData.getDuct_code());
            grouting.setChannelNO(yjData.getMudjack_date());
            grouting.setDirection(uploadParameter.getYajiangfangxiang());
            grouting.setCementCode(uploadParameter.getSnmc());
            grouting.setCementName(uploadParameter.getSnmc());
            grouting.setCementStrength("0");
            grouting.setCementStrength(String.valueOf(yjData.getPractical_volume()));
            grouting.setInitialFlow(Integer.parseInt(yjData.getPractical_fluidity()));
            grouting.setCementFlow(yjData.getPractical_fluidity());
            grouting.setSlurryTemperature(yjData.getSlurry_temperature());
            grouting.setAmbientTemperature(yjData.getEnvironment_temperature());
            grouting.setMixProportion(uploadParameter.getPhb());
            grouting.setStartTime(yjData.getMudjack_date() + " " + yjData.getBegin_time());
            grouting.setEndTime(yjData.getMudjack_date() + " " + yjData.getEnd_time());
            grouting.setFeedPressure(yjData.getMudjack_pressure());
            grouting.setSlurryPressure(yjData.getSlurry_pressure());
            grouting.setResidenceTime(15);
            grouting.setWaterBinderRatio(yjData.getWater_binder_ratio());
            grouting.setPulping("正常");
            grouting.setRemarks("/");
            List<GroutingData> groutingDataList = getGroutingDataList(yjData);
            grouting.setGroutingData(groutingDataList);
            tendonGroutingList.add(grouting);
        }
        return dataList;
    }

    private List<GroutingData> getGroutingDataList(YjData yjData) {
        Map<String, List> graphData = yjDataService.getNewGraphData2(String.valueOf(yjData.getId()));
        List<String> mcgList = graphData.get("mcgList");
        List<String> dateList = graphData.get("dateList");
        List<GroutingData> groutingDataList = new ArrayList<>();
        for (int i = 0; i < dateList.size(); i++) {
            GroutingData groutingData = new GroutingData();
            if (i <= mcgList.size() - 1) {
                groutingData.setPressure(Double.valueOf(mcgList.get(i)));
            } else {
                groutingData.setPressure(0.0);
            }
            groutingData.setDirection(1);
            groutingData.setPressureTime(yjData.getMudjack_date() + " " + dateList.get(i));
            groutingDataList.add(groutingData);
        }
        return groutingDataList;
    }


    public ApiResponse getToken(String url) {
        String response = HttpClients.doGet(url);
        ApiResponse apiResponse = (ApiResponse) JSON.parseObject(StringUtil.replaceBlank(response), ApiResponse.class);
        return apiResponse;
    }


    public ApiResponse sendMessage(String serviceUrl, String json, String token) {
        String result = HttpClients.ezmebRequestPost(serviceUrl, json, token);
        ApiResponse resultJson = (ApiResponse) JSON.parseObject(StringUtil.replaceBlank(result), ApiResponse.class);
        return resultJson;
    }


}
